<?php
//**********************************************************************
//* Script di backup di un database mysql.
//* <info@zenworks.it> 9 marzo 2007
//*
//* Rilasciato con Licenza GPL v.2
//**********************************************************************

//*
//* Config
//*

$db_name = "dbname";
$db_user = "root";
$db_psw = "";
$db_host = "127.0.0.1";
$mail_to = "foo@foo.xx";

$compressione = false; // compressione gz

//*
//* -= MY DUMP =-
//*


$dumpsql="";

function dump($stringa) {
	global $dumpsql;
	$dumpsql.=$stringa."\n";
}

dump("-- MyDump.php ".date("d/m/Y H:i:s"));
dump("");

if ($conn=mysql_connect($db_host, $db_user, $db_psw)) {

	if (mysql_select_db($db_name)) {

		$sql="SHOW TABLE STATUS";
		$stato_tabelle=mysql_query($sql, $conn);
		while($all = mysql_fetch_assoc($stato_tabelle)) {
			$stato_tabella[$all[Name]] = $all[Auto_increment];
		}


		$lista_tabelle=mysql_list_tables($db_name, $conn);

		while ($tabella = mysql_fetch_row($lista_tabelle)) {
			dump("-- Struttura della tabella `".$tabella[0]."`");

			dump("DROP TABLE IF EXISTS `".$tabella[0]."`;");
			$sql="SHOW CREATE TABLE `".$tabella[0]."`";
			$result=mysql_query($sql, $conn);
			$data=mysql_fetch_array($result);
			$dumpdata=$data["Create Table"];
			if ($stato_tabella[$tabella[0]]!="") $dumpdata.=" AUTO_INCREMENT=".$stato_tabella[$tabella[0]].";"; else $dumpdata.=";";

			dump($dumpdata);
			dump("");

			dump("-- dati della tabella `".$tabella[0]."`");
			$sql="SELECT * FROM $tabella[0]";
			$result=mysql_query($sql, $conn);
			$ncampi = mysql_num_fields($result);
			while ($row=mysql_fetch_array($result)) {
				$dumpdata="INSERT INTO `$tabella[0]` VALUES(";

				for ($j=0; $j<$ncampi; $j++) {
					$campo = mysql_field_name($result, $j);
					$flag = mysql_field_flags($result, $j);
					if (strstr($flag, "binary")) {
						$dumpdata.="0x".bin2hex($row[$campo]);
					} else {
						$dumpdata.="'".addslashes($row[$campo])."'";
					}

					if (($j+1)<$ncampi) $dumpdata.=",";
				}

				$dumpdata.=");";
				dump($dumpdata);
			}

			dump("");
		}


	} else {
		dump(mysql_error());
	}

	mysql_close($conn);
} else {
	dump(mysql_error());
}

dump("-- fine dump ".date("d/m/Y H:i:s"));

$boundary=md5(time());

if ($compressione) {
	$attachname=$db_name.".sql.gz";
	$attach=base64_encode(gzencode($dumpsql));
} else {
	$attachname=$db_name.".sql";
	$attach=base64_encode($dumpsql);
}
$text="Il backup del database $db_name � stato richiesto da:\n";
$text.=$_SERVER["REMOTE_ADDR"]."\n".$_SERVER["HTTP_USER_AGENT"]."\n";

$mailfrom=$_SERVER["SERVER_ADMIN"];
$headers = <<<END
From: MyDump <$mailfrom>
Date: $date
X-Mailer: MyDump
MIME-Version: 1.0
Content-Type: multipart/related; boundary="$boundary"
END;

$msg = <<<END
--$boundary
Content-Type: text/plain; charset="iso-9959-1"
Content-Transfer-Encoding: 7bit

$text

--$boundary
Content-Type: octet-stream; name="$attachname"
Content-Disposition: attachment; filename="$attachname"
Content-Transfer-Encoding: base64

$attach

--$boundary--

END;

mail($mail_to, "MyDump.php: $db_name il ".date("d/m/Y H:i:s"), "$msg", $headers);
?>